package com.xhtt.modules.sys.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhtt.common.exception.RRException;
import com.xhtt.common.utils.DeptUtil;
import com.xhtt.modules.sys.dao.SysDeptDao;
import com.xhtt.modules.sys.entity.SysDeptEntity;
import com.xhtt.modules.sys.entity.SysUserDeptEntity;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysDeptService;
import com.xhtt.modules.sys.service.SysUserDeptService;
import com.xhtt.modules.sys.service.SysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {

    @Autowired
    private SysUserDeptService sysUserDeptService;
    @Autowired
    private SysUserService sysUserService;

    @Override
    public List<SysDeptEntity> tree(Map<String, Object> params) {
        Object withoutTeam = params.get("withoutTeam");
        List<SysDeptEntity> deptList = this.list(new QueryWrapper<SysDeptEntity>().eq(SysDeptEntity.PARENTID, 0).eq(null != withoutTeam, SysDeptEntity.TYPE, 1));//先获取一级
        if (deptList.size() < 1) {
            return Collections.EMPTY_LIST;
        }
        //再获取下面的部门
        List<SysDeptEntity> childs = this.list(new QueryWrapper<SysDeptEntity>().gt(SysDeptEntity.PARENTID, 0).eq(null != withoutTeam, SysDeptEntity.TYPE, 1).orderByAsc(SysDeptEntity.ORDERNUM, SysDeptEntity.DEPTID));
        boolean needUser = null == params.get("needUser") ? false : true;
        for (SysDeptEntity role : deptList) {
            DeptUtil.getChildren(role, childs, needUser);
        }
        return deptList;
    }

    @Override
    public void delete(Integer deptId) {
        int count = sysUserDeptService.count(new QueryWrapper<SysUserDeptEntity>().eq(SysUserDeptEntity.DEPTID, deptId));
        if (count > 0) {
            throw new RRException("请先删除人员");
        }
        count = sysUserService.count(new QueryWrapper<SysUserEntity>().eq(SysUserEntity.TEAMID, deptId));
        if (count > 0) {
            throw new RRException("请先删除人员");
        }
        count = this.count(new QueryWrapper<SysDeptEntity>().eq(SysDeptEntity.PARENTID, deptId));
        if (count > 0) {
            throw new RRException("请先删除子项");
        }
        this.removeById(deptId);
    }

    @Override
    public List<SysDeptEntity> teamByDept(Integer deptId) {
        List<SysDeptEntity> list = this.list(new QueryWrapper<SysDeptEntity>().eq(SysDeptEntity.PARENTID, deptId).eq(SysDeptEntity.TYPE, 2));
        return list;
    }

    @Override
    public List<SysDeptEntity> teamList() {

        return baseMapper.selectList(new LambdaQueryWrapper<SysDeptEntity>().eq(SysDeptEntity::getType, 2));
    }

    @Override
    public List<String> getChildDept(Integer deptId) {
        String str = baseMapper.getChildDept(deptId);
        return Arrays.asList(str.split(","));
    }
}
